package com.teacher.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

import com.common.model.Project;

public class StringUtils {

	private static Map<String, String> map = new HashMap<>();

	static {
		Properties p = new Properties();
		try {
			p.load(StringUtils.class.getClassLoader().getResourceAsStream(
					"com/teacher/util/projectStatus.properties"));
			map.put(Project.AUTH_FAILED, p.getProperty(Project.AUTH_FAILED));
			map.put(Project.NOT_AUTH, p.getProperty(Project.NOT_AUTH));
			map.put(Project.PASSED, p.getProperty(Project.PASSED));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
	private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
	private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

	public static String delHTMLTag(String htmlStr) {
		Pattern p_script = Pattern.compile(regEx_script,
				Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll(""); // 过滤script标签
		Pattern p_style = Pattern
				.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll(""); // 过滤style标签
		Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
		Matcher m_html = p_html.matcher(htmlStr);
		htmlStr = m_html.replaceAll(""); //
		// 过滤html标签
		return htmlStr.trim();
		// // 返回文本字符串
	}

	/**
	 * 处理前台传入题目状态的值和需要的值之间的差异 将中文值转换为英文值 若传入的值不在三种状态中，则返回传入的值
	 * 
	 * @param status
	 * @return
	 */
	public static String StatusToProperty(String status) {
		String result = "";
		if (map.containsValue(status)) {
			Set<Entry<String, String>> enties = map.entrySet();
			for (Entry<String, String> entry : enties) {
				if (status.equals(entry.getValue()))
					result = entry.getKey();
			}
		} else {
			result = status;
		}
		return result;
	}

	/**
	 * 将传入的状态值由英文转换为中文
	 * 
	 * @param source
	 * @return
	 */
	public static String propertyToStatus(String source) {
		String result = "";
		if (map.containsKey(source)) {
			Set<Entry<String, String>> entries = map.entrySet();
			for (Entry<String, String> entry : entries) {
				if (source.equals(entry.getKey()))
					result = entry.getValue();
			}
		} else {
			result = source;
		}
		return result;
	}

	/**
	 * 获取项目的web根目录
	 * 
	 * @param request
	 * @return
	 */
	public static String getLocation(HttpServletRequest request) {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";
		return basePath;
	}

	/**
	 * 将含有http协议的网络URL路径转换为本地路径<br/>
	 * 即对应文件在服务器所在的本地文件路径
	 * 
	 * @param url
	 *            需要转换的URL地址
	 * @param request
	 * @return
	 */
	public static String transURLToLocalLocation(String url,
			HttpServletRequest request) {
		String basePath = getLocation(request);
		int index = url.indexOf(basePath);
		String realPath = request.getServletContext().getRealPath("/");
		if (index != -1) {
			String fileName = url.substring(index + basePath.length());
			return realPath + fileName;
		}

		return "";
	}

	public static void setProperties(Project pro, String property,
			List<String> params, Integer index) {
		if (index != null) {
			try {
				BeanUtils.setProperty(pro, property, params.get(index));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static Map<String, Integer> getCols(String[] exportCols) {
		Properties prop = new Properties();
		// String[] cols = new String[exportCols.length];
		Map<String, Integer> map = new HashMap<>();
		try {
			prop.load(StringUtils.class.getClassLoader().getResourceAsStream(
					"com/teacher/util/export.properties"));
			Integer index = 0;
			for (String col : exportCols) {
				Object obj = prop.get(col);
				if (obj != null) {
					// cols[index] = obj.toString();
					map.put(obj.toString(), index);
					index++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	public static Integer getIndexOfElement(String[] arr, String el) {
		Integer index = 0;
		for (String col : arr) {
			if (el.equals(col)) {
				return index;
			} else {
				index++;
			}
		}
		return index;
	}

}
